#!/bin/sh


md5_check(){
    echo "[+]check md5 : .key=$1, .csr=$2, .crt=$3"
    key=$1
    csr=$2
    crt=$3

    echo " *** *** *** "
    echo ".key.pem"
    openssl rsa -noout -modulus -in $key | openssl md5
    #(stdin)= 4a8c8b2d6b1abb9062f940a814724728

    if [ -f "$csr" ];then
        echo ".csr"
        openssl req -noout -modulus -in $csr | openssl md5
        # (stdin)= 4a8c8b2d6b1abb9062f940a814724728
    fi
    
    if [ -f "$crt" ];then
        echo ".crt"
        openssl x509 -noout -modulus -in $crt | openssl md5
        #(stdin)= ef7908413fbc03adb01f87731f47554e
    fi
    
    echo "# # # ====== "
}

dnscheck(){
    echo "==================================="
    openssl rsa -noout  -text -in "$1" | grep -i "dns"
    echo "==================================="
    openssl req -noout  -text -in "$2" | grep -i "dns"
    echo "==================================="
    openssl x509 -noout -text -in "$3" | grep -i "dns"
    echo "####################################"
}

showdetail(){
    echo "=key================="
    openssl rsa -noout  -text -in "$1"
    echo "=csr================="
    openssl req -noout  -text -in "$2"
    echo "=crt================="
    openssl x509 -noout -text -in "$3"
    echo "###############################"
}

printHelp(){
            echo "usage"
            echo "    $0 md5|dns|detail <key> <csr> <crt>"
            echo ""
}

if [ -z "$4" ];then
    printHelp
fi

cmd=$1
server_key=$2
server_csr=$3
server_crt=$4

root_dir="/data"

(
    if [ ! -e "${server_key}" ];then
        cd "${root_dir}"
    fi
    
    case $cmd in
        md5 )
            md5_check "$server_key" "$server_csr" "$server_crt"
            ;;
        dns )
            dnscheck "$server_key" "$server_csr" "$server_crt"
            ;;
        detail )
            showdetail "$server_key" "$server_csr" "$server_crt"
            ;;
        *)
            printHelp
            ;;
    esac

)
